/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvListedepartement;
import fr.cggveritas.crewintranet.jms.entities.CvAffectation;
import fr.cggveritas.crewintranet.jms.entities.CvEquipedepartement;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvEquipedepartementJpaController extends DaoImp implements Serializable {

    public CvEquipedepartementJpaController() {
        super();
    }

    public void create(CvEquipedepartement cvEquipedepartement) {
        if (cvEquipedepartement.getCvAffectationList() == null) {
            cvEquipedepartement.setCvAffectationList(new ArrayList<CvAffectation>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvListedepartement iddepartement = cvEquipedepartement.getIddepartement();
            if (iddepartement != null) {
                iddepartement = em.getReference(iddepartement.getClass(), iddepartement.getIddepartement());
                cvEquipedepartement.setIddepartement(iddepartement);
            }
            List<CvAffectation> attachedCvAffectationList = new ArrayList<CvAffectation>();
            for (CvAffectation cvAffectationListCvAffectationToAttach : cvEquipedepartement.getCvAffectationList()) {
                cvAffectationListCvAffectationToAttach = em.getReference(cvAffectationListCvAffectationToAttach.getClass(), cvAffectationListCvAffectationToAttach.getIdaffectation());
                attachedCvAffectationList.add(cvAffectationListCvAffectationToAttach);
            }
            cvEquipedepartement.setCvAffectationList(attachedCvAffectationList);
            em.persist(cvEquipedepartement);
            if (iddepartement != null) {
                iddepartement.getCvEquipedepartementList().add(cvEquipedepartement);
                iddepartement = em.merge(iddepartement);
            }
            for (CvAffectation cvAffectationListCvAffectation : cvEquipedepartement.getCvAffectationList()) {
                CvEquipedepartement oldCvEquipedepartementIdequipedepartementOfCvAffectationListCvAffectation = cvAffectationListCvAffectation.getCvEquipedepartementIdequipedepartement();
                cvAffectationListCvAffectation.setCvEquipedepartementIdequipedepartement(cvEquipedepartement);
                cvAffectationListCvAffectation = em.merge(cvAffectationListCvAffectation);
                if (oldCvEquipedepartementIdequipedepartementOfCvAffectationListCvAffectation != null) {
                    oldCvEquipedepartementIdequipedepartementOfCvAffectationListCvAffectation.getCvAffectationList().remove(cvAffectationListCvAffectation);
                    oldCvEquipedepartementIdequipedepartementOfCvAffectationListCvAffectation = em.merge(oldCvEquipedepartementIdequipedepartementOfCvAffectationListCvAffectation);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvEquipedepartement cvEquipedepartement) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvEquipedepartement persistentCvEquipedepartement = em.find(CvEquipedepartement.class, cvEquipedepartement.getIdequipedepartement());
            CvListedepartement iddepartementOld = persistentCvEquipedepartement.getIddepartement();
            CvListedepartement iddepartementNew = cvEquipedepartement.getIddepartement();
            List<CvAffectation> cvAffectationListOld = persistentCvEquipedepartement.getCvAffectationList();
            List<CvAffectation> cvAffectationListNew = cvEquipedepartement.getCvAffectationList();
            List<String> illegalOrphanMessages = null;
            for (CvAffectation cvAffectationListOldCvAffectation : cvAffectationListOld) {
                if (!cvAffectationListNew.contains(cvAffectationListOldCvAffectation)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvAffectation " + cvAffectationListOldCvAffectation + " since its cvEquipedepartementIdequipedepartement field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (iddepartementNew != null) {
                iddepartementNew = em.getReference(iddepartementNew.getClass(), iddepartementNew.getIddepartement());
                cvEquipedepartement.setIddepartement(iddepartementNew);
            }
            List<CvAffectation> attachedCvAffectationListNew = new ArrayList<CvAffectation>();
            for (CvAffectation cvAffectationListNewCvAffectationToAttach : cvAffectationListNew) {
                cvAffectationListNewCvAffectationToAttach = em.getReference(cvAffectationListNewCvAffectationToAttach.getClass(), cvAffectationListNewCvAffectationToAttach.getIdaffectation());
                attachedCvAffectationListNew.add(cvAffectationListNewCvAffectationToAttach);
            }
            cvAffectationListNew = attachedCvAffectationListNew;
            cvEquipedepartement.setCvAffectationList(cvAffectationListNew);
            cvEquipedepartement = em.merge(cvEquipedepartement);
            if (iddepartementOld != null && !iddepartementOld.equals(iddepartementNew)) {
                iddepartementOld.getCvEquipedepartementList().remove(cvEquipedepartement);
                iddepartementOld = em.merge(iddepartementOld);
            }
            if (iddepartementNew != null && !iddepartementNew.equals(iddepartementOld)) {
                iddepartementNew.getCvEquipedepartementList().add(cvEquipedepartement);
                iddepartementNew = em.merge(iddepartementNew);
            }
            for (CvAffectation cvAffectationListNewCvAffectation : cvAffectationListNew) {
                if (!cvAffectationListOld.contains(cvAffectationListNewCvAffectation)) {
                    CvEquipedepartement oldCvEquipedepartementIdequipedepartementOfCvAffectationListNewCvAffectation = cvAffectationListNewCvAffectation.getCvEquipedepartementIdequipedepartement();
                    cvAffectationListNewCvAffectation.setCvEquipedepartementIdequipedepartement(cvEquipedepartement);
                    cvAffectationListNewCvAffectation = em.merge(cvAffectationListNewCvAffectation);
                    if (oldCvEquipedepartementIdequipedepartementOfCvAffectationListNewCvAffectation != null && !oldCvEquipedepartementIdequipedepartementOfCvAffectationListNewCvAffectation.equals(cvEquipedepartement)) {
                        oldCvEquipedepartementIdequipedepartementOfCvAffectationListNewCvAffectation.getCvAffectationList().remove(cvAffectationListNewCvAffectation);
                        oldCvEquipedepartementIdequipedepartementOfCvAffectationListNewCvAffectation = em.merge(oldCvEquipedepartementIdequipedepartementOfCvAffectationListNewCvAffectation);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvEquipedepartement.getIdequipedepartement();
                if (findCvEquipedepartement(id) == null) {
                    throw new NonexistentEntityException("The cvEquipedepartement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvEquipedepartement cvEquipedepartement;
            try {
                cvEquipedepartement = em.getReference(CvEquipedepartement.class, id);
                cvEquipedepartement.getIdequipedepartement();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvEquipedepartement with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CvAffectation> cvAffectationListOrphanCheck = cvEquipedepartement.getCvAffectationList();
            for (CvAffectation cvAffectationListOrphanCheckCvAffectation : cvAffectationListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvEquipedepartement (" + cvEquipedepartement + ") cannot be destroyed since the CvAffectation " + cvAffectationListOrphanCheckCvAffectation + " in its cvAffectationList field has a non-nullable cvEquipedepartementIdequipedepartement field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            CvListedepartement iddepartement = cvEquipedepartement.getIddepartement();
            if (iddepartement != null) {
                iddepartement.getCvEquipedepartementList().remove(cvEquipedepartement);
                iddepartement = em.merge(iddepartement);
            }
            em.remove(cvEquipedepartement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvEquipedepartement> findCvEquipedepartementEntities() {
        return findCvEquipedepartementEntities(true, -1, -1);
    }

    public List<CvEquipedepartement> findCvEquipedepartementEntities(int maxResults, int firstResult) {
        return findCvEquipedepartementEntities(false, maxResults, firstResult);
    }

    private List<CvEquipedepartement> findCvEquipedepartementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvEquipedepartement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvEquipedepartement findCvEquipedepartement(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvEquipedepartement.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvEquipedepartementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvEquipedepartement> rt = cq.from(CvEquipedepartement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public List<CvEquipedepartement> findByEquipe(CvListedepartement cvListedepartement) {
        EntityManager em = getEntityManager();
        try {
            String queryString = "select c from CvEquipedepartement c "
                    + "where c.iddepartement = :x";
            Query query = em.createQuery(queryString);
            query.setParameter("x", cvListedepartement);

            return query.getResultList();
        } finally {
            em.close();
        }
    }
    
    
}
